import { request, API_BASE_URL } from '@/utils/http';

// 部门数据类型定义
export interface Department {
  id: number;
  name: string;
  num: number;
}

// 用户数据类型定义
export interface UserInfo {
  admin: number;
  ceo: number;
  department: number;
  hr: number;
  id: number;
  name: string;
  phone: string;
}

// 获取所有部门接口响应类型
export interface GetAllDepartmentsResponse {
  msg: string;
  code: number;
  data: {
    departments: Department[];
    userList: UserInfo[];
  };
}

// 部门成员数据类型定义
export interface DepartmentMember {
  id: number;
  name: string;
  isLeader: boolean;
  position:string,
  url: string;
}

// 获取部门成员接口响应类型
export interface GetDepartmentByIdResponse {
  msg: string;
  code: number;
  data: DepartmentMember[];
}

// 设置主管接口响应类型
export interface SetLeaderResponse {
  msg: string;
  code: number;
}

// 无部门成员数据类型定义
export interface NoDepartmentMember {
  id: number;
  name: string;
  phone: string;
  admin: number;
  department: number;
  ceo: number;
  hr: number;
  url: string;
  position: string;
}

// 获取无部门成员接口响应类型
export interface GetNoDepartmentMembersResponse {
  msg: string;
  code: number;
  data: NoDepartmentMember[];
}

// 添加成员请求数据类型定义
export interface AddMemberRequest {
  id: string;
  memberId: string;
  position: string;
}

// 添加成员接口响应类型
export interface AddMemberResponse {
  msg: string;
  code: number;
}

// 添加部门请求类型
export interface AddDepartmentRequest {
  phone: string;
  remark: string;
  name: string;
  leader: string;
}

// 添加部门接口响应类型
export interface AddDepartmentResponse {
  msg: string;
  code: number;
}

// 部门详情数据类型定义
export interface DepartmentDetailData {
  id: string;
  createDate: string;
  createId: number;
  name: string;
  members: string;
  leader: number;
  phone: string | null;
  remark: string | null;
}

// 获取部门详情接口响应类型
export interface GetDepartmentDetailResponse {
  msg: string;
  code: number;
  data: DepartmentDetailData;
}

// 删除部门接口响应类型
export interface RemoveDepartmentResponse {
  msg: string;
  code: number;
}

/**
 * 获取所有部门数据
 * @returns Promise<GetAllDepartmentsResponse>
 */
export const getAllDepartments = async (): Promise<GetAllDepartmentsResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/findAllDepartment`, {
      method: 'POST',
      data: {}, // 传空对象给后端
    });
    return response;
  } catch (error) {
    console.error('获取部门数据失败:', error);
    throw error;
  }
};

/**
 * 根据部门ID获取部门成员
 * @param id 部门ID
 * @returns Promise<GetDepartmentByIdResponse>
 */
export const findDepartmentById = async (id: string | number): Promise<GetDepartmentByIdResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/findDepartmentById`, {
      method: 'POST',
      data: {
        id: id.toString()
      },
    });
    return response;
  } catch (error) {
    console.error('获取部门成员失败:', error);
    throw error;
  }
};

/**
 * 设置部门主管
 * @param id 部门ID
 * @param leader 主管ID
 * @returns Promise<SetLeaderResponse>
 */
export const setDepartmentLeader = async (id: string | number, leader: number): Promise<SetLeaderResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/setLeader`, {
      method: 'POST',
      data: {
        id: id.toString(),
        leader: leader.toString()
      },
    });
    return response;
  } catch (error) {
    console.error('设置部门主管失败:', error);
    throw error;
  }
};

/**
 * 获取无部门成员列表
 * @returns Promise<GetNoDepartmentMembersResponse>
 */
export const getNoDepartmentMembers = async (): Promise<GetNoDepartmentMembersResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/query/NoDepartment`, {
      method: 'POST',
      data: {},
    });
    return response;
  } catch (error) {
    console.error('获取无部门成员失败:', error);
    throw error;
  }
};

/**
 * 添加成员到部门
 * @param members 成员数据列表，包含部门ID、成员ID和职位信息
 * @returns Promise<AddMemberResponse>
 */
export const addMember = async (members: AddMemberRequest[]): Promise<AddMemberResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/addMember`, {
      method: 'POST',
      data: members,
    });
    return response;
  } catch (error) {
    console.error('添加成员失败:', error);
    throw error;
  }
};

/**
 * 添加部门
 * @param departmentData 部门数据
 * @returns Promise<AddDepartmentResponse>
 */
export const addDepartment = async (departmentData: AddDepartmentRequest): Promise<AddDepartmentResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/addDepartment`, {
      method: 'POST',
      data: departmentData,
    });
    return response;
  } catch (error) {
    console.error('添加部门失败:', error);
    throw error;
  }
};

/**
 * 根据部门ID获取部门详情
 * @param id 部门ID
 * @returns Promise<GetDepartmentDetailResponse>
 */
export const getDepartmentDetail = async (id: string | number): Promise<GetDepartmentDetailResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/query/depatmentById?id=${id}`, {
      method: 'POST'
    });
    return response;
  } catch (error) {
    console.error('获取部门详情失败:', error);
    throw error;
  }
};

/**
 * 删除部门
 * @param id 部门ID
 * @returns Promise<RemoveDepartmentResponse>
 */
export const removeDepartment = async (id: string | number): Promise<RemoveDepartmentResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/removeDepartment`, {
      method: 'POST',
      data: {
        id: id.toString()
      }
    });
    return response;
  } catch (error) {
    console.error('删除部门失败:', error);
    throw error;
  }
};

// 删除主管

export const getRemoveLeader = async (id: string | number): Promise<RemoveDepartmentResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/removeLeader`, {
      method: 'POST',
      data: {
        id: id.toString()
      }
    });
    return response;
  } catch (error) {
    console.error('删除部门失败:', error);
    throw error;
  }
};
// 搜索人员数据类型定义
export interface SearchMember {
  id: number;
  name: string;
  phone: string;
  admin: number;
  department: number;
  ceo: number;
  hr: number;
  url: string;
  position: string | null;
}

// 搜索人员接口响应类型
export interface SearchMembersResponse {
  msg: string;
  code: number;
  data: SearchMember[];
}

// 删除员工接口响应类型
export interface RemoveMemberResponse {
  msg: string;
  code: number;
}

// 员工离职删除接口响应类型
export interface DeleteMemberResponse {
  msg: string;
  code: number;
}

// 更新用户请求数据类型定义
export interface UpdateUserRequest {
  id: number;
  ceo: number;
  hr: number;
}

// 更新用户接口响应类型
export interface UpdateUserResponse {
  msg: string;
  code: number;
}

/**
 * 直接删除员工
 * @param id 组织ID
 * @param member 用户ID
 * @returns Promise<RemoveMemberResponse>
 */
export const removeMember = async (id: string | number, member: string | number): Promise<RemoveMemberResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/removeMember`, {
      method: 'POST',
      data: {
        id: id.toString(),
        member: member.toString()
      }
    });
    return response;
  } catch (error) {
    console.error('删除员工失败:', error);
    throw error;
  }
};

/**
 * 办理离职并删除员工
 * @param id 员工ID
 * @returns Promise<DeleteMemberResponse>
 */
export const deleteMember = async (id: string | number): Promise<DeleteMemberResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/deleteMember?id=${id}`, {
      method: 'POST'
    });
    return response;
  } catch (error) {
    console.error('办理离职删除员工失败:', error);
    throw error;
  }
};

/**
 * 模糊搜索人员信息
 * @param queryLike 搜索关键词
 * @returns Promise<SearchMembersResponse>
 */
export const searchMembers = async (queryLike: string): Promise<SearchMembersResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/query/vague?queryLike=${encodeURIComponent(queryLike)}`, {
      method: 'POST'
    });
    return response;
  } catch (error) {
    console.error('搜索人员信息失败:', error);
    throw error;
  }
};

/**
 * 更新用户CEO/HR权限
 * @param userData 用户数据
 * @returns Promise<UpdateUserResponse>
 */
export const updateUser = async (userData: UpdateUserRequest): Promise<UpdateUserResponse> => {
  try {
    const response = await request(`${API_BASE_URL}/OA/department/updateUser`, {
      method: 'POST',
      data: userData,
    });
    return response;
  } catch (error) {
    console.error('更新用户失败:', error);
    throw error;
  }
};
